EmSAT Achieve Exam  >  EmSAT Achieve Questions  >  What is the output of the following code snip... Start Learning for Free
What is the output of the following code snippet?
public class Example {
    public static void main(String[] args) {
        int result = sum(3, 4);
        System.out.println(result);
    }
    public static int sum(int a, int b) {
        return a + b;
    }
    public static double sum(double a, double b) {
        return a + b;
    }
}
  • a)
    7
  • b)
    7.0
  • c)
    Compilation error
  • d)
    Runtime error
Correct answer is option 'A'. Can you explain this answer?
Most Upvoted Answer
What is the output of the following code snippet?public class Example ...
The 'sum' method in the code snippet is overloaded with two versions: one that takes two integers and another that takes two doubles. In the 'main' method, 'sum(3, 4)' is called, and since the arguments are integers, the first version of 'sum' is invoked, returning the sum of 3 and 4, which is 7.
Free Test
Community Answer
What is the output of the following code snippet?public class Example ...
The output of the given code snippet is 7.

Explanation:
- The code snippet defines two methods with the same name "sum", but with different parameter types.
- In the main method, the sum method is called with two integer arguments: sum(3, 4).
- Since there is a method with matching parameter types (int), the compiler selects the method with int parameters for the method invocation.
- The selected method calculates the sum of the two integer values (3 and 4) and returns the result.
- The returned result (7) is then assigned to the "result" variable.
- Finally, the value of "result" (which is 7) is printed using the System.out.println() method.

Key Points:
- The code snippet demonstrates method overloading, where multiple methods can have the same name but different parameter types.
- The method with matching parameter types is selected for the method invocation based on the arguments passed.
- In this case, the method with int parameters is selected because the arguments passed are integers.
- The selected method performs the addition operation and returns the result, which is then printed.

Therefore, the output of the code snippet is 7.
Attention EmSAT Achieve Students!
To make sure you are not studying endlessly, EduRev has designed EmSAT Achieve study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in EmSAT Achieve.
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer?
Question Description
What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer? for EmSAT Achieve 2024 is part of EmSAT Achieve preparation. The Question and answers have been prepared according to the EmSAT Achieve exam syllabus. Information about What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer? covers all topics & solutions for EmSAT Achieve 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer?.
Solutions for What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for EmSAT Achieve. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.
Here you can find the meaning of What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer?, a detailed solution for What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer? has been provided alongside types of What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What is the output of the following code snippet?public class Example { public static void main(String[] args) { int result = sum(3, 4); System.out.println(result); } public static int sum(int a, int b) { return a + b; } public static double sum(double a, double b) { return a + b; }}a)7b)7.0c)Compilation errord)Runtime errorCorrect answer is option 'A'. Can you explain this answer? tests, examples and also practice EmSAT Achieve tests.
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Explore Courses

Suggested Free Tests

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev